<?php

class Sistema_ProfissaoController extends App_Controller_Action
{
    private $_table     = 'PROFISSAO';
    private $_pk        = 'PRF_ID';
    private $_order_col = 'PRF_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'PRF_STA';
    private $obj        = '';
    private $form       = '';
    
    public function init()
    {
        $this->obj = new Sistema_Model_DbTable_Profissao();
        //if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Profissao();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('elemform','library');
        
        $where = $this->_pk . ' > 0';
        
        //$filtro = $this->_getParam('itfiltro', '1'); 
        //if($filtro != '') $where .= ' and MTL_STA = ' . $filtro;
        //$this->view->filtro = $filtro;
        
        //$STA = $this->_getParam($this->_sta_col, 1);
        //$where .= ' and ' . $this->_sta_col . ' = ' . $STA;
        
        $result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        $this->view->result = $this->paginacao($result);
    }
    
    public function editAction() {
        
        $ajax = $this->_getParam('ajax', '');
        
        if($ajax == 'ajax'){ 
            //$this->_helper->layout->setLayout('layout_clean');
            $this->_helper->layout->disableLayout(); 
            $this->view->ajax = TRUE;
            echo $this->view->InlineScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
            echo $this->view->inlineScript()->appendFile($this->_urlbase.'/public/media/js/view/s_profissao_ajx.js');
        }else if($ajax == ''){ 
            $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
            $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_profissao.js');
        }
        //$this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){//$this->form->isValid($formData)) {
                    $id = (int) $formData[$this->_pk];//$this->form->getValue($this->_pk);
                    
                    $data = $formData;//$this->form->getValues();
                    unset($data['submit']);
                    
                    // valida nome
                    $where = $this->_pk.' > 0';
            
                    if($id > 0) $where .= ' and '.$this->_pk.' <> '.$id;
                    if($data['PRF_NOM'] > '') $where .= ' and PRF_NOM like "'.$data['PRF_NOM'].'"';

                    $result = $this->obj->fetchRow($where);

                    if($result['PRF_NOM'] == $data['PRF_NOM'] || $result[$this->_pk] > 0){
                        $msg = 'Profissão já cadastrada!'; 
                        echo '<script>alert("'.$msg.'");</script>';
                        $this->view->data = $formData;
                    }else{
                   
                        if ($id == 0){ 
                            $this->obj->insert($data); 
                            $id =  $this->obj->getAdapter()->lastInsertId();
                        } 
                        else $this->obj->update($data, $this->_pk.' = ' . $id);

                        if($ajax == 'ajax_submit'){
                            $this->_helper->layout->disableLayout(); 
                            $this->_helper->viewRenderer->setNoRender(); 
                            echo json_encode(array('ok' => true, 'id' => $id));
                        }else{
                            $this->_helper->redirector('index');
                        }
                    }
                } else {
                    //$this->form->populate($formData);
                    $this->view->data = $formData;
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                //$this->form->populate($formData);
                $this->view->data = $formData;
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                //$this->form->populate($formData);
                $this->view->data = $data;
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            $data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
            $ok = (bool) $this->obj->delete($this->_pk . ' = ' . $id);
        }       
        echo json_encode(array('ok' => $ok));
    }
    
    public function validaformAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        $nome = $this->_getParam('nome', '');
        $msg = ''; 
        if($nome > '' ){   
            
            $where = $this->_pk.' > 0';
            
            if($id > 0) $where .= ' and '.$this->_pk.' <> '.$id;
            if($nome > '') $where .= ' and PRF_NOM like "'.$nome.'"';
            
            $result = $this->obj->fetchRow($where);
            
            if($result['PRF_NOM'] == $nome || $result[$this->_pk] > 0){
                $ok = TRUE;
                $msg = 'Profissão já cadastrada!'; 
            }
        }       
        echo json_encode(array('ok' => $ok,'msg'=>$msg));
    }
    
    
    public function comboprofissaoAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = TRUE;
        $html = '';
        $id = $this->_getParam('id', 0);
        $name = $this->_getParam('name', 0);

        $where = $this->_pk.' > 0';
        $result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        
        $html .= '<strong>Profissão:</strong> <select id="'.$name.'" name="'.$name.'" class="chosen-select" required=""><option value="0">Nenhum</option>';
        foreach($result as $res){
            $selected = ($id == $res->PRF_ID) ? 'selected=""':'';
            $html .= '<option value="'.$res->PRF_ID.'" '.$selected.'>'.$res->PRF_NOM.'</option>';
        }
        $html .= '</select> <a href="javascript:void(0)" class="edit_profissao">+</a>';
        $html .= '<script>$(function(){ $(".chosen-select").chosen(); });</script>';
        echo json_encode(array('ok' => $ok,'html'=>$html));
    }

}